﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using blackhouse.BlackApart.Business;
using blackhouse.BlackApart.Business.Apartments;
using blackhouse.BlackApart.Business.Cities;
using blackhouse.BlackApart.Business.Prices;
using blackhouse.BlackApart.Business.Reservations;
using blackhouse.BlackApart.Web;
using blackhouse.BlackApart.Web.Navigation;
using blackhouse.JavaScript.JQuery;

namespace blackhouse.Aparts.Web.Pages {
    public partial class OnlineReservationStatus : System.Web.UI.Page {

        #region Fields

        private DateTime dtArrival;
        private DateTime dtDeparture;
        private int apartmentGroupId;
        private int selectedPeoples;
        private bool postedParams;

        private Factory factory;
        private IApartmentGroup group;
        private Variant variant;

        #endregion

        #region Event Handlers

        protected override void OnInit(EventArgs e) {
            base.OnInit(e);

            this.ReadInputParameters();

        }

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            this.SetInstance();

            if (this.postedParams) {
                if (this.variant != null)
                    this.PopulateControlsForApartment();
                else
                    this.PopulateControlsForBusyApartments();

                SearchState state = SearchState.GetState(this.dtArrival, this.dtDeparture, this.selectedPeoples);
                this.lnkToOthersApartments.NavigateUrl = NavigationProvider.Instance().GetUrl(state);
            }

            JQueryLibraries jql = new JQueryLibraries();
            jql.IncludeFileBasedOnJQuery("blackhouse.Aparts.Web.Scripts.online-reservation-status.js,blackhouse.Aparts.Web");
        }

        #endregion

        #region Methods

        private void SetInstance() {
            this.factory = Factory.Instance();
            if (this.postedParams) {
                this.group = this.factory.GetApartmentGroup(this.apartmentGroupId);

                FreeApartments search = new FreeApartments(this.dtArrival, this.dtDeparture, this.selectedPeoples);
                search.FilterToApartmentType(this.apartmentGroupId);
                this.variant = search.FirstOrDefault();
            }
        }

        private void PopulateControlsForBusyApartments() {
            this.content.Visible = true;
            this.pnlFreeApartments.Visible = false;
            this.pnlNoFreeApartments.Visible = true;
        }

        private void PopulateControlsForApartment() {
            this.content.Visible = true;
            if (this.selectedPeoples <= this.group.NumberOfPeople) {
                this.pnlPeopleOuter.Width = Unit.Pixel(this.group.NumberOfPeople * 11);
                this.pnlPeople.Width = Unit.Pixel(this.selectedPeoples * 11);
            }
            else {
                this.pnlPeople.Width = Unit.Pixel(this.group.NumberOfPeople * 11);
            }
            string lnkPeople_tooltip = GetGlobalResourceObject("Aparts", "apartmentsPeopleCount").ToString();
            if (!string.IsNullOrEmpty(lnkPeople_tooltip) && lnkPeople_tooltip.Contains("{0}"))
                this.lnkPeople.ToolTip = string.Format(lnkPeople_tooltip, this.group.NumberOfPeople);

            int nights = (int)(this.dtDeparture.Date - this.dtArrival.Date).TotalDays;
            string nightText = nights + " ";
            if (nights == 1)
                nightText += GetGlobalResourceObject("Aparts", "nights1");
            if (nights > 1 && nights <= 4)
                nightText += GetGlobalResourceObject("Aparts", "nights24");
            if (nights > 4)
                nightText += GetGlobalResourceObject("Aparts", "nightsmore");
            this.lblNight.Text = nightText;

            if (variant != null) {
                this.lblPriceAll.Value = this.variant.Price.GetPrice();
                this.lblPricePersonNight.Value = this.variant.Price.GetPricePerNight() / this.selectedPeoples;
                if (variant.Price.GetRabatValue() < 0) {
                    this.ltlTotalCost.Visible = false;
                    this.ltlNewPrice.Visible = true;
                    this.ltlRabatName.Text = this.variant.Price.GetAssociacedRabats()[0].RabatName;
                    this.ltlRabatValue.Text = string.Format("{0}%", this.variant.Price.GetRabatValue());
                    this.lblOldPrice.Value = this.variant.Price.GetPriceWithoutRabat();
                    this.pnlDiscount.Visible = true;
                }
                this.lnkToReservation.NavigateUrl = NavigationProvider.Instance().GetUrl(ReservationPage.GetInformationPage(variant.GetGroups().First().Id, SearchState.GetState(this.dtArrival, this.dtDeparture, this.selectedPeoples)));
            }
        }

        private bool ReadInputParameters() {
            if (!DateTime.TryParseExact(Request["arrival"], "dd.MM.yyyy", CultureInfo.CurrentCulture, DateTimeStyles.None, out dtArrival))
                return false;
            if (!DateTime.TryParseExact(Request["departure"], "dd.MM.yyyy", CultureInfo.CurrentCulture, DateTimeStyles.None, out dtDeparture))
                return false;
            if (!Int32.TryParse(Request["apartment"], out this.apartmentGroupId))
                return false;
            if (!Int32.TryParse(Request["peoples"], out this.selectedPeoples))
                return false;
            this.postedParams = true;
            return true;
        }

        #endregion

    }
}